package com.gaogzhen.threadPool.scheduledThreadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "c.TestScheduledThreadPool02")
public class TestScheduledThreadPool02 {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        log.debug("start...");
        method2(pool);

    }

    public static void method1(ScheduledExecutorService pool) {
        pool.scheduleAtFixedRate(() -> {
            log.debug("task 1 running...");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1, 1, TimeUnit.SECONDS);
    }

    public static void method2(ScheduledExecutorService pool) {

        pool.scheduleWithFixedDelay(() -> {
            log.debug("task 2 running...");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1, 1, TimeUnit.SECONDS);
    }


}
